<template>
  <el-form
    :model="form"
    :rules="rules"
    ref="formRef"
    label-width="80px"
    @submit.prevent
  >
    <el-form-item label="访客名称" prop="name">
      <el-input v-model="form.name" />
    </el-form-item>
    <el-form-item label="留言内容" prop="content">
      <el-input v-model="form.content" type="textarea" />
    </el-form-item>
    <el-form-item label="主页链接" prop="url">
      <el-input v-model="form.url" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">提交</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { ref } from 'vue'
import api from '../api'

const emit = defineEmits(['refresh'])
const form = ref({ name: '', content: '', url: '' })
const rules = {
  name: [{ required: true, message: '请输入访客名称', trigger: 'blur' }],
  content: [{ required: true, message: '请输入留言内容', trigger: 'blur' }],
}
const formRef = ref()

const onSubmit = async () => {
  await formRef.value.validate()
  await api.post('/messages', form.value)
  form.value = { name: '', content: '', url: '' }
  emit('refresh')
}
</script>
